﻿namespace WMS6.Web.Inventory
{
    using System;
    using System.Collections.Generic;
    using System.Web.UI;
    using System.Web.UI.HtmlControls;
    using System.Web.UI.WebControls;
    using WMS6.Web;
    using WMS6.Web.InboundServiceRef;
    using WMS6.Web.InventoryMgtServiceRef;
    using WMS6.Web.CommonMethodServiceRef;

    public class AdjustmentDetail : BasePage
    {
        protected Button btnCancel;
        protected Button btnSave;
        protected DropDownList ddlReasonCode;
        protected DropDownList ddlUomID;
        protected HtmlForm F5033;
        protected HtmlInputHidden hdnDescr;
        protected HtmlInputHidden hdnFrom;
        protected HtmlInputHidden hdnLoc;
        protected HtmlInputHidden hdnLot;
        protected HtmlInputHidden hdnLpnID;
        protected HtmlInputHidden hdnOwnerID;
        protected HtmlInputHidden hdnPackID;
        protected HtmlInputHidden hdnQty;
        protected HtmlInputHidden hdnReceiptID;
        protected HtmlInputHidden hdnSkuID;
        protected HtmlInputHidden hdnUomQty;
        protected HtmlHead Head1;
        protected HtmlImage imgLoc;
        protected HtmlImage imgSkuID;
        protected Label lblAdjustmentLineID;
        protected Label lblASNDetail;
        protected Label lblDescr;
        protected Label lblLoc;
        protected Label lblLot;
        protected Label lblLpnID;
        protected Label lblMsg;
        protected Label lblPackID;
        protected Label lblQty;
        protected Label lblQty1;
        protected Label lblQty2;
        protected Label lblReasonCode;
        protected Label lblRemark;
        protected Label lblSkuID;
        protected Label lblUomID;
        protected HtmlLink mycss;
        protected Label one1;
        protected HtmlAnchor packID;
        protected ScriptManager ScriptManager1;
        protected TextBox txtAdjustmentLineID;
        protected TextBox txtDescr;
        protected TextBox txtLoc;
        protected TextBox txtLot;
        protected TextBox txtLpnID;
        protected TextBox txtPackID;
        protected TextBox txtQty;
        protected TextBox txtQty1;
        protected TextBox txtQty2;
        protected TextBox txtRemark;
        protected TextBox txtSkuID;

        private void BindData2UomList(string packID)
        {
            try
            {
                this.txtLoc.Text = this.hdnLoc.Value;
                this.txtLot.Text = this.hdnLot.Value;
                this.txtLpnID.Text = this.hdnLpnID.Value;
                this.txtDescr.Text = this.hdnDescr.Value;
                this.txtQty.Text = this.hdnQty.Value;
                this.txtSkuID.Text = this.hdnSkuID.Value;
                this.txtPackID.Text = this.hdnPackID.Value;
                List<UomList> uomList = CommoneMethodServiceClientProxy.GetInstance().GetUomListByPackID(packID);
                if ((uomList != null) && (uomList.Count > 0))
                {
                    base.BindUomList2DDL(this.ddlUomID, uomList);
                    this.ViewState["UOMLIST"] = uomList;
                    this.CalcSkuBaseUomQty();
                }
            }
            catch (Exception ex)
            {
                this.lblMsg.Text = base.GetGlobalMsgResource("F5150_012") + ex.Message;
                this.txtPackID.Focus();
            }
        }

        protected void btnCancel_Click(object sender, EventArgs e)
        {
            base.Response.Redirect(string.Format("Adjustment.aspx?from={0}&detail={1}&ID={2}", this.hdnFrom.Value, "1", base.Server.UrlEncode(this.hdnReceiptID.Value.Trim())));
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            try
            {
                if (!string.IsNullOrEmpty(this.txtQty1.Text.Trim()))
                {
                    if ((decimal.Parse(base.GetPreferenceDigit(this.txtQty.Text.Trim())) + decimal.Parse(this.txtQty1.Text.Trim())) < 0M)
                    {
                        this.lblMsg.Text = base.GetGlobalMsgResource("F5150_005");
                        this.lblMsg.Visible = true;
                        return;
                    }
                }
                else
                {
                    this.lblMsg.Text = base.GetGlobalMsgResource("F5150_006");
                    this.lblMsg.Visible = true;
                    return;
                }
                if (string.IsNullOrEmpty(this.txtLoc.Text))
                {
                    this.txtLoc.Focus();
                    this.lblMsg.Text = base.GetGlobalMsgResource("F5150_007");
                    this.lblMsg.Visible = true;
                }
                else if (string.IsNullOrEmpty(this.txtPackID.Text))
                {
                    this.txtPackID.Focus();
                    this.lblMsg.Text = base.GetGlobalMsgResource("F5150_008");
                    this.lblMsg.Visible = true;
                }
                else if (string.IsNullOrEmpty(this.txtSkuID.Text))
                {
                    this.txtSkuID.Focus();
                    this.lblMsg.Text = base.GetGlobalMsgResource("F5150_009");
                    this.lblMsg.Visible = true;
                }
                else if (string.IsNullOrEmpty(this.txtQty1.Text.Trim()) || (int.Parse(this.txtQty1.Text.Trim()) == 0))
                {
                    this.txtQty1.Focus();
                    this.lblMsg.Text = base.GetGlobalMsgResource("F5150_006");
                    this.lblMsg.Visible = true;
                }
                else if (string.IsNullOrEmpty(this.ddlUomID.SelectedValue))
                {
                    this.ddlUomID.Focus();
                    this.lblMsg.Text = base.GetGlobalMsgResource("F5150_010");
                    this.lblMsg.Visible = true;
                }
                else
                {
                    this.InsertAdjustmentDetailInfo();
                }
            }
            catch (Exception ex)
            {
                this.lblMsg.Text = ex.Message;
            }
        }

        private void CalcAdjustmentQty()
        {
            this.lblMsg.Text = string.Empty;
            if (decimal.Parse(this.txtQty.Text) > 0M)
            {
                if (decimal.Parse(this.txtQty1.Text) > 0M)
                {
                    this.txtQty2.Text = (decimal.Parse(this.txtQty.Text) + this.CalcSkuBaseUomQty()).ToString();
                }
                else if ((decimal.Parse(this.txtQty.Text) + this.CalcSkuBaseUomQty()) >= 0M)
                {
                    this.txtQty2.Text = base.GetPreferenceDigit((decimal) (decimal.Parse(this.txtQty.Text) + this.CalcSkuBaseUomQty()));
                }
                else
                {
                    this.lblMsg.Text = base.GetGlobalMsgResource("F5150_013");
                    this.lblMsg.Visible = true;
                }
            }
            else
            {
                List<string> initLocal0 = new List<string> {
                    this.txtQty.Text
                };
                this.lblMsg.Text = base.GetGlobalMsgResource("F5150_014", initLocal0);
                this.lblMsg.Visible = true;
                this.txtQty1.Text = "0";
            }
        }

        private decimal CalcSkuBaseUomQty()
        {
            decimal temp = 0M;
            List<UomList> uomList = this.ViewState["UOMLIST"] as List<UomList>;
            float uomQty = base.GetUomQtyByUomList(this.ddlUomID.SelectedValue, uomList);
            if (!((uomQty <= 0f) || string.IsNullOrEmpty(this.txtQty1.Text.Trim())))
            {
                temp = Convert.ToDecimal((float) (float.Parse(this.txtQty1.Text.Trim()) * uomQty));
            }
            return temp;
        }

        protected void ddlUomID_SelectedIndexChanged(object sender, EventArgs e)
        {
            this.CalcAdjustmentQty();
        }

        private InventoryMgtServiceRef.AdjustmentDetailInfo GetAdjustmentDetailInfo()
        {
            InventoryMgtServiceRef.AdjustmentDetailInfo adjustment;
            return new InventoryMgtServiceRef.AdjustmentDetailInfo { AdjustmentDate = DateTime.Now, AdjustmentID = this.hdnReceiptID.Value, LineID = int.Parse(this.txtAdjustmentLineID.Text), SkuID = this.txtSkuID.Text, Loc = this.txtLoc.Text, Qty = decimal.Parse(this.txtQty1.Text), ReasonCode = this.ddlReasonCode.SelectedValue, PackID = this.txtPackID.Text, Remark = this.txtRemark.Text, UomID = this.ddlUomID.SelectedValue, Lot = this.txtLot.Text, LpnID = this.txtLpnID.Text, OwnerID = this.hdnOwnerID.Value, CreatedBy = base.UserInfo.UserID, UpdatedBy = base.UserInfo.UserID };
        }

        private void InsertAdjustmentDetailInfo()
        {
            try
            {
                string strAdjumentID = string.Empty;
                strAdjumentID = InventoryMgtServiceClientProxy.GetInstance().ExecuteInvAdjustment(this.GetAdjustmentDetailInfo());
                if (!string.IsNullOrEmpty(strAdjumentID))
                {
                    this.lblMsg.Text = strAdjumentID + this.txtAdjustmentLineID.Text + base.GetGlobalMsgResource("C004");
                    base.Server.Transfer("Adjustment.aspx?from={0}&detail={1}&ID=" + base.UrlEncode(strAdjumentID), false);
                }
            }
            catch (Exception ex)
            {
                if (ex.Message.Contains("CK_LOT_QTY"))
                {
                    this.lblMsg.Text = base.GetGlobalMsgResource("F5150_011");
                    this.lblMsg.Visible = true;
                }
                else
                {
                    this.lblMsg.Text = ex.Message;
                    this.lblMsg.Visible = true;
                }
            }
        }

        private void LoadData()
        {
            base.BindData2DDLBySysCode(this.ddlReasonCode, "AdjReason");
            this.txtAdjustmentLineID.Text = base.Request.QueryString["LienID"];
            this.hdnReceiptID.Value = base.Request.QueryString["ID"];
            this.hdnOwnerID.Value = base.Request.QueryString["OwnerID"];
            if (base.Request.QueryString["type"] == "Edit")
            {
                WMS6.Web.InboundServiceRef.AdjustmentDetailInfo adjustmentDetailInfo = (base.PageQueryEntitySession as WMS6.Web.InboundServiceRef.AdjustmentInfo).AdjustmentDetail[0];
                this.txtDescr.Text = adjustmentDetailInfo.Descr;
                this.hdnDescr.Value = adjustmentDetailInfo.Descr;
                this.txtDescr.Enabled = false;
                this.hdnLoc.Value = adjustmentDetailInfo.Loc;
                this.txtLoc.Text = adjustmentDetailInfo.Loc;
                this.txtLoc.Enabled = false;
                this.imgLoc.Disabled = true;
                this.txtLot.Text = adjustmentDetailInfo.Lot;
                this.hdnLot.Value = adjustmentDetailInfo.Lot;
                this.txtLot.Enabled = false;
                this.txtLpnID.Text = adjustmentDetailInfo.LpnID;
                this.hdnLpnID.Value = adjustmentDetailInfo.LpnID;
                this.txtLpnID.Enabled = false;
                this.txtPackID.Text = adjustmentDetailInfo.PackID;
                this.hdnPackID.Value = adjustmentDetailInfo.PackID;
                this.txtPackID.Enabled = false;
                this.txtQty.Text = base.GetPreferenceDigit(0);
                this.hdnQty.Value = this.txtQty.Text;
                this.txtQty.Enabled = false;
                this.txtRemark.Text = adjustmentDetailInfo.Remark;
                this.txtRemark.Enabled = false;
                this.txtSkuID.Text = adjustmentDetailInfo.SkuID;
                this.hdnSkuID.Value = adjustmentDetailInfo.SkuID;
                this.txtSkuID.Enabled = false;
                this.imgSkuID.Disabled = true;
                this.txtPackID_OnTextChanged(null, null);
                this.txtPackID.Enabled = false;
                this.ddlUomID.SelectedValue = adjustmentDetailInfo.UomID;
                this.ddlUomID.Enabled = false;
                this.txtAdjustmentLineID.Text = adjustmentDetailInfo.LineID.ToString();
                this.txtAdjustmentLineID.Enabled = false;
                this.btnSave.Enabled = false;
                this.ddlReasonCode.SelectedValue = adjustmentDetailInfo.ReasonCode;
                this.ddlReasonCode.Enabled = false;
                this.txtQty1.Text = base.GetPreferenceDigit(adjustmentDetailInfo.Qty);
                this.txtQty1.Enabled = false;
                this.txtQty2.Text = base.GetPreferenceDigit(0);
                this.imgLoc.Visible = false;
                this.imgSkuID.Visible = false;
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!base.IsPostBack)
            {
                base.InitPage(this.Page);
                this.LoadData();
            }
        }

        private void SetPageinvLptLpcLpn(WMS6.Web.InventoryMgtServiceRef.InvLotLocLpnInfo invLotLocLpnInfo)
        {
            this.txtLoc.Text = invLotLocLpnInfo.Loc;
            this.txtLot.Text = invLotLocLpnInfo.Lot;
            this.txtLpnID.Text = invLotLocLpnInfo.LpnID;
            this.txtQty.Text = Convert.ToInt32(invLotLocLpnInfo.Qty).ToString();
            this.txtSkuID.Text = invLotLocLpnInfo.SkuID;
        }

        protected void txtPackID_OnTextChanged(object sender, EventArgs e)
        {
            this.BindData2UomList(this.hdnPackID.Value.Trim());
        }

        protected void txtQty1_OnTextChanged(object sender, EventArgs e)
        {
            this.CalcAdjustmentQty();
        }
    }
}

